(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open XNum
  
type colour =
  | Grey of num
  | RGB of num * num * num
  | CMYK of num * num * num * num
 (* with sexp *)

type 'a bezier = ('a * 'a * 'a * 'a * 'a * 'a * 'a * 'a) (* with sexp *)
type 'a path = ('a bezier) list (* with sexp *)

type path_cmd  = | Stroke | Fill   | Clip   (* with sexp *)
type line_cap  = | Butt   | Circle | Square (* with sexp *)
type line_join = | Miter  | Round  | Bevel  (* with sexp *)

type ('dim, 'box) graphic_command =
  | PutBox of 'dim * 'dim * 'box
  | Draw of path_cmd * 'dim path
  | SetColour of colour
  | SetBgColour of colour
  | SetAlpha of num
  | SetLineWidth of num
  | SetLineCap of line_cap
  | SetLineJoin of line_join
  | SetMiterLimit of num
 (* with sexp *)

let command_to_string cmd =
  match cmd with
    | PutBox (_, _, _) -> "put-box"
    | Draw (pc, _) ->
        begin
          match pc with
            | Stroke -> "stroke"
            | Fill -> "fill"
            | Clip -> "clip"
        end
    | SetColour _ -> "set-colour"
    | SetBgColour _ -> "set-bg-colour"
    | SetAlpha _ -> "set-alpha"
    | SetLineWidth _ -> "set-line-width"
    | SetLineCap _ -> "set-line-cap"
    | SetLineJoin _ -> "set-line-join"
    | SetMiterLimit _ -> "set-miter-limit"
  
let compare_colour c1 c2 =
  match (c1, c2) with
    | (Grey g1, Grey g2) -> compare_num g1 g2
    | (Grey _, _) -> (-1)
    | (_, Grey _) -> 1
    | (RGB (r1, g1, b1), RGB (r2, g2, b2)) ->
        if lt_num r1 r2 then
          (-1)
        else if gt_num r1 r2 then
          1
        else if lt_num g1 g2 then
          (-1)
        else if gt_num g1 g2 then
          1
        else if lt_num b1 b2 then
          (-1)
        else if gt_num b1 b2 then
          (-1)
        else
          0
    | (RGB (_, _, _), _) -> (-1)
    | (_, RGB (_, _, _)) -> 1
    | (CMYK (c1, m1, y1, k1), CMYK (c2, m2, y2, k2)) ->
        if lt_num c1 c2 then
          (-1)
        else if gt_num c1 c2 then
          1
        else if lt_num m1 m2 then
          (-1)
        else if gt_num m1 m2 then
          1
        else if lt_num y1 y2 then
          (-1)
        else if gt_num y1 y2 then
          (-1)
        else if lt_num k1 k2 then
          (-1)
        else if gt_num k1 k2 then
          (-1)
        else
          0

(*-----------------------------------------------------------------------*)
